t = codesters.Teacher()
tm = TestManager()
stage.set_axis(0,10)
""" ONLY CHANGE WHAT IS BETWEEN THE GREEN DASHES:
---------------------------------------------------------------"""
# This array represents the board.
# 1's are blocks and 0's are open pathways.
maze_data = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
# This is where the turtle starts.
start_position = (4, 6)
# This is where the goal is.
goal_position = (5, 6)
# This is the direction the turtle points to begin with
start_direc = 0
# This is the scale factor for the blocks.
block_size = 1
end_result = "incomplete"
# This function iterates through the maze_data array and draws the squares.
def draw_board():
board = maze_data
for j in range(len(board)):
for i in range(len(board[j])):
if board[j][i] == 1:
block_x = (i + 0.5) * block_size
block_y = (j + 0.5) * block_size
block = codesters.Square(block_x, block_y, block_size, "sienna", "black")
else:
block_x = (i + 0.5) * block_size
block_y = (j + 0.5) * block_size
block = codesters.Square(block_x, block_y, block_size, None, "black")
# This function creates a goal block at the specified position.
def create_goal():
goal_x = (goal_position[0] - 0.5) * block_size
goal_y = (goal_position[1] - 0.5) * block_size
goal_block = codesters.Square(goal_x, goal_y, block_size, "gold", "black")
# This is the main program. It calls the three principal functions,
# creates the turtle, and puts it on the screen
# This calls the function to draw the goal
# This line calls the function to draw the whole board
draw_board()
create_goal()
txt = codesters.Text("Guide the hedgehog to the gold box.", 5, 9.5, "white")
sprite = codesters.Sprite("hedgehog")
sprite.set_size(0.5)
sprite.set_speed(2)
# This function puts the hedgehog at the starting position and rotation.
def send_sprite_home(t):
t.set_x((start_position[0] - 0.5 ))
t.set_y((start_position[1] - .5) * block_size)
t.turn_left(start_direc)
send_sprite_home(sprite)
try:
tval1 = sprite.get_x()
tval2 = sprite.get_y()
except:
tval1 = "DNE"
tval2 = "DNE"
if tval1 == (goal_position[0]) and tval2 == (goal_position[1]):
stage.remove_sprite(txt)
tm.display_success_message("Great job!")
elif tval1 == start_position[0] and tval2 == start_position[1]:
txt.set_text("Guide the hedgehog to the gold box.")
else:
tm.display_failure_message("Oops! You hit a block. Try again!")